﻿Public Class Criptografia

    Private Sub btnCriptografar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCriptografar.Click
        If Me.lstTipos.SelectedIndex = -1 Then
            MsgBox("Selecione um tipo de criptografia.")
            Exit Sub
        End If

        Select Case Me.lstTipos.SelectedItem.ToString
            'Criptografa com o algorítmo RC2
            Case "RC2"
                Try
                    Dim RC2 As New MNT.Criptografia.RC2
                    Me.txtCriptografado.Text = RC2.Criptografa(Me.txtTexto.Text, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Criptografa com o algorítmo DES
            Case "DES"
                Try
                    Dim DES As New MNT.Criptografia.DES
                    Me.txtCriptografado.Text = DES.Criptografa(Me.txtTexto.Text, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Criptografa com o algorítmo TripleDES
            Case "TripleDES"
                Try
                    Dim TripleDES As New MNT.Criptografia.TripleDES
                    Me.txtCriptografado.Text = TripleDES.Criptografa(Me.txtTexto.Text, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Criptografa com o algorítmo Rexor
            Case "Rexor"
                Try
                    Dim Rexor As New MNT.Criptografia.Rexor
                    Me.txtCriptografado.Text = Rexor.CriptografaDescriptografa(Me.txtTexto.Text, Me.txtChave.Text)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo HMAC-MD5
            Case "HMAC-MD5"
                Try
                    Dim Cripto As New MNT.Hash.HMACMD5
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo HMAC-RIPEMD160
            Case "HMAC-RIPEMD160"
                Try
                    Dim Cripto As New MNT.Hash.HMACRIPEMD160
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo HMAC-SHA1
            Case "HMAC-SHA1"
                Try
                    Dim Cripto As New MNT.Hash.HMACSHA
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, MNT.Hash.HMACSHA.Tipo.Normal, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo HMAC-SHA256
            Case "HMAC-SHA256"
                Try
                    Dim Cripto As New MNT.Hash.HMACSHA
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, MNT.Hash.HMACSHA.Tipo.Criptografia256, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo HMAC-SHA384
            Case "HMAC-SHA384"
                Try
                    Dim Cripto As New MNT.Hash.HMACSHA
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, MNT.Hash.HMACSHA.Tipo.Criptografia384, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo HMAC-SHA512
            Case "HMAC-SHA512"
                Try
                    Dim Cripto As New MNT.Hash.HMACSHA
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, MNT.Hash.HMACSHA.Tipo.Criptografia512, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo MD5
            Case "MD5"
                Try
                    Dim Cripto As New MNT.Hash.MD5
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo RIPEMD160
            Case "RIPEMD160"
                Try
                    Dim Cripto As New MNT.Hash.RIPEMD160
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo SHA1
            Case "SHA1"
                Try
                    Dim Cripto As New MNT.Hash.SHA
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, MNT.Hash.SHA.Tipo.Normal, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo SHA256
            Case "SHA256"
                Try
                    Dim Cripto As New MNT.Hash.SHA
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, MNT.Hash.SHA.Tipo.Criptografia256, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo SHA384
            Case "SHA384"
                Try
                    Dim Cripto As New MNT.Hash.SHA
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, MNT.Hash.SHA.Tipo.Criptografia384, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Gera hash com o Algorítmo SHA512
            Case "SHA512"
                Try
                    Dim Cripto As New MNT.Hash.SHA
                    Me.txtCriptografado.Text = Cripto.GeraString(Me.txtTexto.Text, MNT.Hash.SHA.Tipo.Criptografia512, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

        End Select

    End Sub

    Private Sub btnDescriptografar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDescriptografar.Click
        If Me.lstTipos.SelectedIndex = -1 Then
            MsgBox("Selecione um tipo de criptografia.")
            Exit Sub
        End If

        Select Case Me.lstTipos.SelectedItem.ToString
            'Criptografa com o algorítmo RC2
            Case "RC2"
                Try
                    Dim RC2 As New MNT.Criptografia.RC2
                    Me.txtTexto.Text = RC2.Descriptografa(Me.txtCriptografado.Text, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Criptografa com o algorítmo DES
            Case "DES"
                Try
                    Dim DES As New MNT.Criptografia.DES
                    Me.txtTexto.Text = DES.Descriptografa(Me.txtCriptografado.Text, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Criptografa com o algorítmo TripleDES
            Case "TripleDES"
                Try
                    Dim TripleDES As New MNT.Criptografia.TripleDES
                    Me.txtTexto.Text = TripleDES.Descriptografa(Me.txtCriptografado.Text, Me.txtChave.Text, Me.chkHexa.Checked)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

                'Criptografa com o algorítmo Rexor
            Case "Rexor"
                Try
                    Dim Rexor As New MNT.Criptografia.Rexor
                    Me.txtTexto.Text = Rexor.CriptografaDescriptografa(Me.txtCriptografado.Text, Me.txtChave.Text)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

            Case Else
                MsgBox("Esta é uma chave hash e não pode ser descriptografada.")
        End Select


    End Sub

End Class